# -*- tcl -*-
# bibtex.test:  tests for the bibtex parser.
#
# Copyright (c) 2005 by Andreas Kupries <a.kupries@westend.com>
# All rights reserved.

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.4
testsNeedTcltest 1.0

testing {
    useLocal bibtex.tcl bibtex
}

# -------------------------------------------------------------------------

proc track {args} {global track ; lappend track $args ; return}
proc addstr {token strings} {
    track string__     $token $strings
    bibtex::addStrings $token $strings
}

# -------------------------------------------------------------------------

test bibtex-1.0 {Parse errors} {
    set code [catch {::bibtex::parse} msg]
    list $code $msg
} {1 {wrong # args: should be "::bibtex::parse ?options? ?bibtex?"}}

test bibtex-1.1 {Parse errors} {} {
    set code [catch {::bibtex::parse -frob} msg]
    list $code $msg
} {1 {bad option "frob", should be one of -casesensitivestrings, -channel, -command, -commentcommand, -preamblecommand, -progresscommand, -recordcommand, or -stringcommand}}

test bibtex-1.2 {Parse errors} {
    set code [catch {::bibtex::parse -frob nibar} msg]
    list $code $msg
} {1 {bad option "frob", should be one of -casesensitivestrings, -channel, -command, -commentcommand, -preamblecommand, -progresscommand, -recordcommand, or -stringcommand}}

test bibtex-1.3 {Parse errors} {} {
    set code [catch {::bibtex::parse -frob nibar fuzz} msg]
    list $code $msg
} {1 {bad option "frob", should be one of -casesensitivestrings, -channel, -command, -commentcommand, -preamblecommand, -progresscommand, -recordcommand, or -stringcommand}}

test bibtex-1.4 {Parse errors} {} {
    set code [catch {::bibtex::parse -command nibar -recordcommand fuzz snarf} msg]
    list $code $msg
} {1 {The options -command and -TYPEcommand exclude each other}}

test bibtex-1.5 {Parse errors} {} {
    set code [catch {::bibtex::parse -channel nibar snarf} msg]
    list $code $msg
} {1 {Option -channel and text exclude each other}}

test bibtex-1.6 {Parse errors} {} {
    set code [catch {::bibtex::parse -channel nibar} msg]
    list $code $msg
} {1 {Illegal channel handle "nibar"}}

test bibtex-1.7 {Parse errors} {} {
    set code [catch {::bibtex::parse -command nibar} msg]
    list $code $msg
} {1 {Neither -channel nor text specified}}

test bibtex-1.8 {Parse errors} {} {
    set code [catch {::bibtex::parse -command nibar fuzz snarf} msg]
    list $code $msg
} {1 {wrong # args: ::bibtex::parse ?options? ?bibtex?}}

test bibtex-1.9 {Parse errors} {} {
    set code [catch {::bibtex::parse -command nibar fuzz} msg]
    list $code $msg
} {1 {Option -command and text exclude each other}}


# -------------------------------------------------------------------------

set bytecode [list [list \
	book krasner83 [list \
	title     {Smalltalk-80: Bits of History, Words of Advice} \
	publisher Addison-Wesley \
	year      1983 \
	editor    {Glen Krasner} \
	]]]

set penn [list [list \
	inproceedings Carberry90 [list \
	author    {Sandra Carberry} \
	title     {Incorporating default inferences into plan recognition} \
	booktitle aaai90 \
	year      1990 \
	pages     471--478 \
	address   {Boston, MA} \
	]]]

set pennfull [list [list \
	inproceedings Carberry90 [list \
	author    {Sandra Carberry} \
	title     {Incorporating default inferences into plan recognition} \
	booktitle {Proc. National Conference on Artificial Intelligence, Boston} \
	year      1990 \
	pages     471--478 \
	address   {Boston, MA} \
	]]]


test bibtex-2.0 {Parse string, direct result} {
    set str [viewFile [asset bytecode.bib]]
    bibtex::parse $str
} $bytecode

test bibtex-2.1 {Parse string, sax mode} {
    set track {}
    set str [viewFile [asset bytecode.bib]]
    set t [bibtex::parse \
	    -progresscommand {track progress} \
	    -commentcommand  {track comment_} \
	    -stringcommand   {track string__} \
	    -preamblecommand {track preamble} \
	    -recordcommand   {track record__} \
	    $str]
    bibtex::destroy $t
    list $t $track
} [list bibtex2 [list \
	{progress bibtex2 100} \
	[linsert [lindex $bytecode 0] 0 record__ bibtex2]
]]

test bibtex-2.2 {Parse channel, direct result} {
    # The contents of penn_sub.bib have been taken out of
    # ftp://ftp.cis.upenn.edu/pub/anoop/bib/pennbib.bib

    set chan    [open [asset penn_sub.bib] r]
    set records [bibtex::parse -channel $chan]
    close $chan
    set records
} $pennfull

test bibtex-2.3 {Parse channel, sax mode} {
    set track {}
    set chan [open [asset penn_sub.bib] r]

    set t [bibtex::parse \
	    -progresscommand {track progress} \
	    -commentcommand  {track comment_} \
	    -stringcommand   {track string__} \
	    -preamblecommand {track preamble} \
	    -recordcommand   {track record__} \
	    -channel $chan]
    bibtex::wait    $t
    bibtex::destroy $t

    close $chan
    set track
} [list \
	{progress bibtex4 50} \
	{string__ bibtex4 {aaai90 {Proc. National Conference on Artificial Intelligence, Boston}}} \
	{progress bibtex4 100} \
	[linsert [lindex $penn 0] 0 record__ bibtex4] \
	]

test bibtex-2.4 {Parse channel, sax mode 2} {
    set track {}
    set chan [open [asset penn_sub.bib] r]

    set t [bibtex::parse \
	    -progresscommand {track progress} \
	    -commentcommand  {track comment_} \
	    -stringcommand   addstr \
	    -preamblecommand {track preamble} \
	    -recordcommand   {track record__} \
	    -channel $chan]
    bibtex::wait    $t
    bibtex::destroy $t
    close $chan
    set track
} [list \
	{progress bibtex5 50} \
	{string__ bibtex5 {aaai90 {Proc. National Conference on Artificial Intelligence, Boston}}} \
	{progress bibtex5 100} \
	[linsert [lindex $pennfull 0] 0 record__ bibtex5] \
	]

test bibtex-2.5 {Parse channel, async} {
    # The contents of penn_sub.bib have been taken out of
    # ftp://ftp.cis.upenn.edu/pub/anoop/bib/pennbib.bib

    set chan [open [asset penn_sub.bib] r]
    proc done {args} {global done ; set done $args ; return}

    set done ""
    set t [bibtex::parse -command done -channel $chan]
    vwait ::done
    bibtex::destroy $t
    close $chan
    set done
} [list bibtex6 $pennfull]


test bibtex-3.0 {Destroying a parser} {
    set code [catch {::bibtex::destroy} msg]
    list $code $msg
} [list 1 [tcltest::wrongNumArgs "::bibtex::destroy" "token" 0]]

test bibtex-3.1 {Destroying a parser} {
    set code [catch {::bibtex::destroy a b} msg]
    list $code $msg
} [list 1 [tcltest::tooManyArgs "::bibtex::destroy" "token"]]

test bibtex-3.2 {Destroying a parser} {
    set code [catch {::bibtex::destroy foo} msg]
    list $code $msg
} {1 {Illegal bibtex parser "foo"}}

test bibtex-4.0 {Destroying a parser} {

    set chan [open [asset bytecode.bib] r]
    proc done {args} {global done ; set done $args ; return}

    set done ""
    set t [bibtex::parse -command done -channel $chan]
    bibtex::destroy $t
    close $chan
} {}

# ... Tests of addStrings ...
#     (Requires introspection of parser state)

testsuiteCleanup
